package com.nf.health.app.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;

public abstract class MyBaseAdapter<T> extends BaseAdapter implements OnScrollListener{

	protected LayoutInflater mInflater;
	protected Collection<T> mDatas;
	protected final int mItemLayoutId;
	protected AbsListView mList;
	protected boolean isScrolling;

	private OnScrollListener listener;

	public MyBaseAdapter(AbsListView view, Collection<T> mDatas, int itemLayoutId) {
		this.mInflater = LayoutInflater.from(view.getContext());
		if (mDatas == null) {
			mDatas = new ArrayList<T>(0);
		}
		this.mDatas = mDatas;
		this.mItemLayoutId = itemLayoutId;
		this.mList = view;
		mList.setOnScrollListener(this);
	}

	public void refresh(Collection<T> datas) {
		if (datas == null) {
			datas = new ArrayList<T>(0);
		}
		this.mDatas = datas;
		notifyDataSetChanged();
	}

	public void addOnScrollListener(OnScrollListener l) {
		this.listener = l;
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public T getItem(int position) {
		if (mDatas instanceof List) {
			return ((List<T>) mDatas).get(position);
		} else if (mDatas instanceof Set) {
			return new ArrayList<T>(mDatas).get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final AdapterHolder viewHolder = getViewHolder(position, convertView,
				parent);
		convert(viewHolder, getItem(position), isScrolling);
		return viewHolder.getConvertView();

	}

	private AdapterHolder getViewHolder(int position, View convertView,
			ViewGroup parent) {
		return AdapterHolder.get(convertView, parent, mItemLayoutId, position);
	}

	public abstract void convert(AdapterHolder holder, T item,
			boolean isScrolling);

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// 设置是否滚动的状态
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
			isScrolling = false;
			this.notifyDataSetChanged();
		} else {
			isScrolling = true;
		}
		if (listener != null) {
			listener.onScrollStateChanged(view, scrollState);
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (listener != null) {
			listener.onScroll(view, firstVisibleItem, visibleItemCount,
					totalItemCount);
		}
	}

}
